package com.langll.mcp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.langll.mcp.base.service.IBaseService;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Transactional//开启声明式事务注解，为该类添加事务支持。
public abstract class BaseServiceImpl<Mapper extends BaseMapper, Entity> implements IBaseService<Entity> {

    private Class<Entity> entityClass;
    protected Mapper mapper;

    public BaseServiceImpl(Mapper mapper){
        entityClass = (Class<Entity>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        this.mapper = mapper;
    }
    @Override
    public int insert(Entity entity) {
        return mapper.insert(entity);
    }

    @Override
    public int deleteById(Serializable id) {
        return mapper.deleteById(id);
    }

    @Override
    public int deleteById(Entity entity) {
        return mapper.deleteById(entity);
    }

    @Override
    public int deleteByMap(Map<String, Object> columnMap) {
        return mapper.deleteByMap(columnMap);
    }

    @Override
    public int delete(Wrapper<Entity> queryWrapper) {
        return mapper.delete(queryWrapper);
    }

    @Override
    public int deleteBatchIds(Collection<?> idList) {
        return mapper.deleteBatchIds(idList);
    }

    @Override
    public int updateById(Entity entity) {
        return mapper.updateById(entity);
    }

    @Override
    public int update(Entity entity, Wrapper<Entity> updateWrapper) {
        return mapper.update(entity, updateWrapper);
    }

    @Override
    public Entity selectById(Serializable id) {
        return (Entity) mapper.selectById(id);
    }

    @Override
    public List<Entity> selectBatchIds(Collection<? extends Serializable> idList) {
        return mapper.selectBatchIds(idList);
    }

    @Override
    public void selectBatchIds(Collection<? extends Serializable> idList, ResultHandler<Entity> resultHandler) {
        mapper.selectBatchIds(idList,resultHandler);
    }

    @Override
    public List<Entity> selectByMap(Map<String, Object> columnMap) {
        return mapper.selectByMap(columnMap);
    }

    @Override
    public Long selectCount(Wrapper<Entity> queryWrapper) {
        return mapper.selectCount(queryWrapper);
    }

    @Override
    public List<Entity> selectList(Wrapper<Entity> queryWrapper) {
        return mapper.selectList(queryWrapper);
    }

    @Override
    public void selectList(Wrapper<Entity> queryWrapper, ResultHandler<Entity> resultHandler) {
        mapper.selectList(queryWrapper, resultHandler);
    }

    @Override
    public List<Entity> selectList(IPage<Entity> page, Wrapper<Entity> queryWrapper) {
        return List.of();
    }

    @Override
    public void selectList(IPage<Entity> page, Wrapper<Entity> queryWrapper, ResultHandler<Entity> resultHandler) {
        mapper.selectMaps(queryWrapper, resultHandler);
    }

    @Override
    public List<Map<String, Object>> selectMaps(Wrapper<Entity> queryWrapper) {
        return mapper.selectMaps(queryWrapper);
    }

    @Override
    public void selectMaps(Wrapper<Entity> queryWrapper, ResultHandler<Map<String, Object>> resultHandler) {
        mapper.selectMaps(queryWrapper, resultHandler);
    }

    @Override
    public List<Map<String, Object>> selectMaps(IPage<? extends Map<String, Object>> page, Wrapper<Entity> queryWrapper) {
        return List.of();
    }

    @Override
    public void selectMaps(IPage<? extends Map<String, Object>> page, Wrapper<Entity> queryWrapper, ResultHandler<Map<String, Object>> resultHandler) {
        mapper.selectMaps(page, queryWrapper, resultHandler);
    }

    @Override
    public <E> List<E> selectObjs(Wrapper<Entity> queryWrapper) {
        return List.of();
    }

    @Override
    public <E> void selectObjs(Wrapper<Entity> queryWrapper, ResultHandler<E> resultHandler) {
        mapper.selectObjs(queryWrapper, resultHandler);
    }

    @Override
    public <P extends IPage<Entity>> P selectPage(P page, Wrapper<Entity> queryWrapper) {
        return (P) mapper.selectPage(page, queryWrapper);
    }

    @Override
    public <P extends IPage<Map<String, Object>>> P selectMapsPage(P page, Wrapper<Entity> queryWrapper) {
        return (P) mapper.selectMapsPage(page, queryWrapper);
    }
}
